# _*_coding:utf-8 _*_
#@Time    :2019/3/27 12:56
#@Author  :Dapan
# @Email : 248312738@qq.com
import pymysql
from urllib.parse import unquote



# mysql 配置
MYSQL_HOST="localhost"
# MYSQL_HOST="127.0.0.1"
MYSQL_PORT=3306
MYSQL_USER="root"
MYSQL_PASSWORD="123456"
MYSQL_DB="park4night"
MYSQL_CHARSET='utf8'
MYSQL_TABLE_NAME='data'


class MysqlDB():
    def __init__(self):
        # 打开数据库连接
        self.db = pymysql.connect(
            host=MYSQL_HOST,
            port=MYSQL_PORT,
            user=MYSQL_USER,
            passwd=MYSQL_PASSWORD,
            db=MYSQL_DB,
            # charset=MYSQL_CHARSET
        )
        # 通过cursor方法获取操作游标
        self.cursor = self.db.cursor()
        # sql语句
        self.insterData = "insert into %s(%s) values %s"
        self.query_sql = "SELECT SiteCode FROM {tab_name} WHERE {field}={val};"
        self.query_all_field_sql = "SELECT {field} FROM {tab_name}"
        self.update = "update %s set %s=%s where %s='%s'"




    def query_(self, tab_name, field, val):
        # 执行sql语句
        self.cursor.execute(self.query_sql.format(tab_name=tab_name, field=field, val=val))
        # 使用fetchone方法获取一个查询结果集
        data = self.cursor.fetchall()  # fetch:获取
        return data


    def query_all_field(self, field, tab_name):
        # 执行sql语句
        self.cursor.execute(self.query_all_field_sql.format(field=field, tab_name=tab_name))
        # 使用fetchone方法获取一个查询结果集
        data = self.cursor.fetchall()  # fetch:获取
        sit_list = []
        for i in data:
            sit_list.append(i[0])

        return sit_list

    def insert_(self, table, field_list, rowdata):
        # 执行sql语句
        # print(self.insterData%(table, field_list, rowdata))

        self.cursor.execute(self.insterData%(table, field_list, rowdata))
        # 使用fetchone方法获取一个查询结果集
        self.db.commit()


    def __del__(self):
        # 关闭数据库连接
        self.db.close()




mydb = MysqlDB()

if __name__ == '__main__':
    db = MysqlDB()
    db.cursor.execute(db.query_all_field_sql.format(field='*', tab_name='data'))

    while True:
        row_data = db.cursor.fetchone()
        # print(row_data)
        if not row_data:
            break
        else:
            province = row_data[9]
            print(province)
            if province:
                province_ = unquote(province, 'utf-8')

